<?php

class Stat extends Controller {

	function index()
	{
		$this->load->view('head', array());
		$this->load->view('foot');
	}

	function move_money()
	{
		$d1 = date("j", time() - 7*24*60*60);
		$m1 = date("n", time() - 7*24*60*60);
		$y1 = date("Y", time() - 7*24*60*60);
		$d2 = date("j");
		$m2 = date("n");
		$y2 = date("Y");
		$data = array('d1' => $d1, 'm1' => $m1, 'y1' => $y1, 'd2' => $d2, 'm2' => $m2, 'y2' => $y2);
		if(@$_REQUEST['d1'])
		{
			$d1 = intval($_REQUEST['d1']);
			$m1 = intval($_REQUEST['m1']);
			$y1 = intval($_REQUEST['y1']);
			$d2 = intval($_REQUEST['d2']);
			$m2 = intval($_REQUEST['m2']);
			$y2 = intval($_REQUEST['y2']);
			$data = array('d1' => $d1, 'm1' => $m1, 'y1' => $y1, 'd2' => $d2, 'm2' => $m2, 'y2' => $y2);
			$date1 = dateToDb($y1."-".$m1."-".$d1);
			$date2 = dateToDb($y2."-".$m2."-".$d2);
			$users = $this->MUser->getChildrenAndMe(Auth::$user_id);
			$allsumm = array();
			$all_platezh_id = array();
			foreach($users as $k => $user)
			{
				$user['operations'] = $this->MOper->getList(array('user_id' => $user['id'], 'date>=' => $date1, 'date<=' => $date2, 'bonus' => 0, 'type' => array('buy', 'sell', 'sell.mass', 'week'), 'sort' => 'date asc,id asc'));
				foreach($user['operations'] as $oper)
				{
					if(!isset($allsumm[$oper['valuta']])) $allsumm[$oper['valuta']] = array();
					$t = isBuy($oper) ? 'buy' : 'sell';
					if(!isset($allsumm[$oper['valuta']][$t])) $allsumm[$oper['valuta']][$t] = 0;
					$allsumm[$oper['valuta']][$t] += $oper['summ'];
					$all_platezh_id[] = $oper['platezh_id'];
				}
				$users[$k] = $user;
			}
			$data['allsumm'] = $allsumm;

			if(!count($all_platezh_id))
			{
				$all_platezh_id = -1;
			}
			$all_platezhi = $this->MPlatezh->getList(array('t.id' => $all_platezh_id, 'add_fields' => array('schet_name')));
			foreach($users as $k => $u)
			{
				foreach($u['operations'] as $kk => $o)
				{
					$users[$k]['operations'][$kk]['_platezh'] = @$all_platezhi[$o['platezh_id']];
				}
			}
			$data['users'] = $users;
		}
		$this->load->view('head', array());
		$this->load->view('stat/move_money', $data);
		$this->load->view('foot');
	}

	function fin_report()
	{
		$d1 = date("j", time() - 7*24*60*60);
		$m1 = date("n", time() - 7*24*60*60);
		$y1 = date("Y", time() - 7*24*60*60);
		$d2 = date("j");
		$m2 = date("n");
		$y2 = date("Y");
		$data = array('d1' => $d1, 'm1' => $m1, 'y1' => $y1, 'd2' => $d2, 'm2' => $m2, 'y2' => $y2);
		if(@$_REQUEST['d1'])
		{
			$d1 = intval($_REQUEST['d1']);
			$m1 = intval($_REQUEST['m1']);
			$y1 = intval($_REQUEST['y1']);
			$d2 = intval($_REQUEST['d2']);
			$m2 = intval($_REQUEST['m2']);
			$y2 = intval($_REQUEST['y2']);
			$data = array('d1' => $d1, 'm1' => $m1, 'y1' => $y1, 'd2' => $d2, 'm2' => $m2, 'y2' => $y2);
			$date1 = dateToDb($y1."-".$m1."-".$d1);
			$date2 = dateToDb($y2."-".$m2."-".$d2);
			$users = $this->MUser->getChildrenAndMe(Auth::$user_id);
			$opers = $this->MOper->getList(array('date>=' => $date1, 'date<=' => $date2, 'user_id' => array_keys($users)));
			$bonus_summ = array();
			$bonus_mavro = array();
			$vyplata = array();
			$summ_kro = array();
			foreach($opers as $oper)
			{
				if(!$oper['bonus'] && ($oper['type'] == 'sell' || $oper['type'] == 'sell.mass'))
				{
					if(!isset($vyplata[$oper['valuta']])) $vyplata[$oper['valuta']] = 0;
					$vyplata[$oper['valuta']] += floatval(number_format($oper['summ'], 2, '.', ''));
				}
				if(substr($oper['type'], 0, 2) == 'l*' && $oper['user_id'] == Auth::$user_id)
				{
					$key = $oper['type'] == 'l*' ? "*" : ($oper['type'] == 'l**' ? "**" : "***");
					if(!isset($bonus_summ[$oper['valuta']])) $bonus_summ[$oper['valuta']] = array();
					if(!isset($bonus_summ[$oper['valuta']][$key])) $bonus_summ[$oper['valuta']][$key] = 0;
					$bonus_summ[$oper['valuta']][$key] += $oper['summ'];
					if(!isset($bonus_mavro[$oper['mavro_valuta']])) $bonus_mavro[$oper['mavro_valuta']] = array();
					if(!isset($bonus_mavro[$oper['mavro_valuta']][$key])) $bonus_mavro[$oper['mavro_valuta']][$key] = 0;
					$bonus_mavro[$oper['mavro_valuta']][$key] += $oper['mavro'];
				}
				if(!$oper['bonus'] && ($oper['type'] == 'buy' || $oper['type'] == 'week'))
				{
					if(!isset($summ_kro[$oper['valuta']])) $summ_kro[$oper['valuta']] = 0;
					$summ_kro[$oper['valuta']] += floatval(number_format($oper['summ'], 2, '.', ''));
				}
			}
			$data['vyplata'] = $vyplata;
			$data['bonus_summ'] = $bonus_summ;
			$data['bonus_mavro'] = $bonus_mavro;
			$data['summ_kro'] = $summ_kro;
			$data['me'] = $this->MUser->get(Auth::$user_id);
		}
		$this->load->view('head', array());
		$this->load->view('stat/fin_report', $data);
		$this->load->view('foot');
	}

	function _calc($time, $now, $percent, $summ)
	{
		$date = date("Y-m-d", $time);
		$mavro = $summ / $this->MKurs->getKurs(date("Y-m-d", $now), $percent, 'buy');
		$summ2 = $mavro * $this->MKurs->getKurs($date, $percent, 'sell');
		return number_format($summ2, 0, '', ' ');
	}

	function calc()
	{
		$data = array();
		$data['day'] = @$_GET['day'] ? $_GET['day'] : date('j');
		$data['month'] = @$_GET['month'] ? $_GET['month'] : date('n');
		$data['year'] = @$_GET['year'] ? $_GET['year'] : date('Y');
		$data['now'] = mktime(0, 0, 0, $data['month'], $data['day'], $data['year']);
		$data['time1'] = strtotime("+1 month, + 1day", $data['now']);
		$data['time3'] = strtotime("+3 months, + 1day", $data['now']);
		$data['time6'] = strtotime("+6 months, + 1day", $data['now']);
		$data['time9'] = strtotime("+9 months, + 1day", $data['now']);
		$data['time12'] = strtotime("+1 year, + 1day", $data['now']);
		$data['summ'] = floatval(str_replace(",", ".", str_replace(" ", "", @$_GET['summ'])));
		$data['ng'] = @$_GET['ng'] ? true : false;
		if($data['summ'])
		{
			$s = $data['summ'] * ($data['ng'] ? 1.25 : 1);
			$data['summ2'] = number_format($s, 0, '', ' ');
			$result = array();
			for($i = 20; $i <= 60; $i+=10)
			{
				$result[$i][1] = $this->_calc($data['time1'], $data['now'], $i, $s);
				$result[$i][3] = $this->_calc($data['time3'], $data['now'], $i, $s);
				$result[$i][6] = $this->_calc($data['time6'], $data['now'], $i, $s);
				$result[$i][9] = $this->_calc($data['time9'], $data['now'], $i, $s);
				$result[$i][12] = $this->_calc($data['time12'], $data['now'], $i, $s);
			}
			for($i = 45; $i <= 75; $i+=10)
			{
				$result[$i][3] = $this->_calc($data['time3'], $data['now'], $i, $s);
				$result[$i][6] = $this->_calc($data['time6'], $data['now'], $i, $s);
				$result[$i][9] = $this->_calc($data['time9'], $data['now'], $i, $s);
				$result[$i][12] = $this->_calc($data['time12'], $data['now'], $i, $s);
			}
			$data['result'] = $result;
		}
		$this->load->view('head', array());
		$this->load->view('stat/calc', $data);
		$this->load->view('foot');
	}

	function mavro_kurs()
	{
		$this->load->view('head', array());
		$this->load->view('stat/mavro_kurs');
		$this->load->view('foot');
	}

	function valuta_kurs()
	{
		global $VALUTA;
		$all_exchanges = $this->MKurs->getAllExchange();
		$exchanges_date = array();
		foreach($all_exchanges as $row)
		{
			$key_valuta = $row['valuta'];
			$exchanges_date[$row['date']][$row['valuta']] = $row['kurs'];
			$exchanges[$row['date']][$key_valuta." / ".$VALUTA['R']] = $row['kurs'];
		}
		foreach($exchanges_date as $date => $kurses)
		{
			foreach($VALUTA as $k => $v)
			{
				if($k == 'R') continue;
				foreach($VALUTA as $kk => $vv)
				{
				}
			}
		}
		$data = array('exchanges' => $exchanges_date);
		$this->load->view('head', array());
//		$this->load->view('stat/valuta_kurs', $data);
		$this->load->view('foot');
	}

}
?>